import { observable, action,runInAction ,toJS} from "mobx";




// 分类数据
export default class Type{

  // treeData
  @observable treeData=[];
  // 表单数据
  @observable form={insert:{},update:{}};
  // 选择上级分类
  @observable options=[];
  // 被选树形节点
  @observable info=false;
  // selectedKeys
  @observable selectedKeys=[];
  @observable loadedKeys=[];

  constructor(root){
    this.root = root;
  }
  
  @action init=()=>{
    this._init();
  }

  @action set=(k,v)=>{
    const arr = k.split("-");
    const loop = (obj,arr,v)=>{
      const temp = arr.splice(0,1);
      if(arr.length){
        if(!obj[temp]){obj[temp]={};}
        obj = obj[temp];
        loop(obj,arr,v);
      }else{
        obj[temp] = v;
      }
    };
    loop(this,arr,v);
  }
  
  @action find=(upid,func)=>this._find(upid,func);




  // 点击节点
  @action onSelect = (selectedKeys,info)=>{
    this.set("selectedKeys",selectedKeys);
    if(!info.selected){
      this._init_form();
      this.selectedNodes=false;
      return;
    }
    // console.log("info",info)
    this.set("info",info);
    const dataRef = info.node;
    this.set("form-update",{
      id:dataRef.id,
      tit:dataRef.tit,
      upid:dataRef.upid,
      uptit:dataRef.uptit,
      tp:dataRef.tp,
      cont:dataRef.cont,
      image:dataRef.image
    });
    this.set("form-insert-upid",dataRef.key);
    this.set("form-insert-uptit",dataRef.uptit);
  };

  // insert
  @action insert=()=>{
    this.root.set("loading",true);
    this.root.ajax.post({
      url:this.root.url,
      data:{
        act:"admin-type-insert",
        tit:this.form.insert.tit,
        upid:this.form.insert.upid,
        tp:this.form.insert.tp,
        cont:this.form.insert.cont,
        image:this.form.insert.image,
      },
      success:json=>{
        this.root.set("loading",false);
        this.root.modal.info({
          title:"提示",
          content:json.msg
        });
        if(json.status === "ok"){
          const upid = this.form.insert.upid;
          const uptit = this.form.insert.uptit;
          this.set("form-insert-upid",upid);
          this.set("form-insert-uptit",uptit);
          if(json.data.id){
            if(json.data.id){
              runInAction(()=>{
                let children = this.info.node.children;
                children=children?children:[];
                children.push(
                  Object.assign(
                    json.data,
                    {
                      key:json.data.id,
                      title:json.data.tit,
                      isLeaf: json.data.num==="0"
                    }
                  )
                );
                this.info.node.children=children;
              });
              this.set("treeData",[...this.treeData]);
            }
          }
        }
      }
    });
  };

  // update
  @action update=()=>{
    this.root.set("loading",true);
    this.root.ajax.post({
      url:this.root.url,
      data:{
        act:"admin-type-update",
        id:this.form.update.id,
        tit:this.form.update.tit,
        upid:this.form.update.upid,
        tp:this.form.update.tp,
        cont:this.form.update.cont,
        image:this.form.update.image,
      },
      success:json=>{
        this.root.set("loading",false);
        this.root.modal.info({
          title:"提示",
          content:json.msg
        });
        if(json.status === "ok"){
          const update  = this.form.update;
          this._init();
          this.set("form-update",update);
          this.set("form-insert-upid",update.id);
          this.set("form-insert-uptit",update.tit);
        }
      }
    });
  };

  // delete
  @action delete=()=>{
    this.root.set("loading",true);
    this.root.ajax.post({
      url:this.root.url,
      data:{
        act:"admin-type-delete",
        id:this.form.update.id
      },
      success:json=>{
        this.root.set("loading",false);
        this.root.modal.info({
          title:"提示",
          content:json.msg
        });
        if(json.status === "ok"){
          this.set("selectedKeys",[]);
          this._init();
        }
      }
    });
  };
  
  _init(){
    this._init_form();
    this.set("treeData",[]);
    this.set("options",[]);
    this.set("info",false);
    this.set("selectedKeys",[]);
    this.set("loadedKeys",[]);
    this.set("treeWidth",300);
    this._init_data();
  }
  
  _init_data(){
    this._find(0,json=>{
      this.root.set("loading",false);
      if(json.status !== "ok"){
        this.root.modal.info({
          title:"提示",
          content:json.msg
        });
        return;
      }
      const treeData = json.data.map(item=>Object.assign({key:"".concat(item.id),title:item.tit,isLeaf: false},item));
      this.set("treeData",[{tit:"顶级分类",id:0,upid:0,cont:"顶级分类",title:"顶级分类",key:"0",children:treeData}]);

      const options=json.data.map(item=>Object.assign({value:item.id,label:item.tit,isLeaf: false},item));
      this.set("options",[{label:"顶级分类",value:"0",children:options}]);
    });
  }
  _init_form(){
    this.set("form",{
      insert:{
        tit:"",
        upid:"",
        uptit:"",
        tp:"",
        cont:"",
        image:""
      },
      update:{
        id:"",
        tit:"",
        upid:"",
        uptit:"",
        tp:"",
        cont:"",
        image:""
      }
    });
  }

  // 加载子节点
  @action onLoadData = treeNode =>{
    return new Promise((resolve) => {
      if (treeNode.children) {
        resolve();
        return;
      }
      this.root.set("loading",true);
      this._find(treeNode.id,json=>{
        this.root.set("loading",false);
        if(json.status !== "ok"){
          this.root.modal.info({
            title:"提示",
            content:json.msg
          });
          resolve();
          return;
        }
        const children = json.data.map(
          item=>Object.assign(
            {
              key:item.id,
              title:item.tit,
              isLeaf:item.num===1?true:false
            },
            item
          )
        );
        treeNode.props.data.children=children;
        runInAction(()=>this.set("treeData",[...this.treeData]));
        resolve();
      });
    });
  };


  // 加载下拉菜单
  @action onLoadOption = selectedOptions => this._onLoadOption(selectedOptions);
  _onLoadOption(selectedOptions) {
    const targetOption = selectedOptions[selectedOptions.length - 1];
    targetOption.loading = true;
    this.root.set("loading",true);
    this._find(targetOption.id,json=>{
      targetOption.loading = false;
      this.root.set("loading",false);
      if(json.status !== "ok"){
        this.root.modal.info({
          title:"提示",
          content:json.msg
        });
        return;
      }
      if(json.data.length){
        runInAction(()=>{
          targetOption.children = json.data.map(item=>Object.assign({value:item.id,label:item.tit,isLeaf: (Number(item.num)===0),},item));  
        });
        runInAction(()=>this.set("options",[...this.options]));
      }
    });
  }

  _find(upid,func){
    this.root.set("loading",true);
    this.root.ajax.post({
      url:this.root.url,
      data:{act:"admin-type-select",upid:upid},
      success:func
    });
  }

    // 上传图片
  @action uploadChange=(info,tp)=>{
    console.log(info);
    if(info.file.status === "uploading"){
      this.root.set("loading",true);
    }else if (info.file.status === "done"){
      this.root.set("loading",false);
      this.set("form-".concat(tp),info.file.response.data);
    }else if (info.file.status === "error") {
      this.root.alert({status:"error","msg":`${info.file.name} 图片上传失败!.`});
      this.root.set("loading",false);
    }else{
      this.root.set("loading",false);
    }
  }

}